﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using System.Collections.Generic;
using Yqd.Enm;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxJobPlanController : Controller
    {
        YqdContext ctx;
        DdService ddService;

        public WxJobPlanController(YqdContext ctx, DdService ddService)
        {
            this.ctx = ctx;
            this.ddService = ddService;
        }

        /// <summary>
        /// 添加办理反馈
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Add()
        {
            var TaskID = Form.GetLong("TaskID");
            var SuperviseID = Form.GetInt("SuperviseID");
            var CoordinateID = Form.GetInt("CoordinateID");
            var eSuperviseID = Form.GetInt("eSuperviseID");
            var sSuperviseID = Form.GetInt("sSuperviseID");
            var AssignID = Form.GetInt("AssignID");
            var datas = Form.GetString("datas").ToJArray();
            var ItemID = SuperviseID;
            var ItemType = 事项类型.钉办督导;
            string headtitle = "", text = "";
            var IsDd = false;
            if (SuperviseID > 0)
            {
                var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                if (s != null)
                {
                    headtitle = "领导督导";
                    text = "督导意见  " + s.Content;
                }
            }
            if (CoordinateID > 0)
            {
                ItemID = CoordinateID;
                ItemType = 事项类型.申请协调;
                var c = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CoordinateID == ItemID);
                if (c != null)
                {
                    headtitle = "领导协调";
                    text = "协调内容  " + c.Item;
                }
            }
            if (eSuperviseID > 0)
            {
                ItemID = eSuperviseID;
                ItemType = 事项类型.精准督查;
                IsDd = true;
                var s = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                if (s != null)
                {
                    headtitle = "精准督查";
                    text = "存在问题  " + s.Problem;
                }
            }
            if (sSuperviseID > 0)
            {
                ItemID = sSuperviseID;
                ItemType = 事项类型.专项督查;
                IsDd = true;
                var s = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                if (s != null)
                {
                    headtitle = "专项督查";
                    text = "存在问题  " + s.Problem;
                }
            }
            switch (ItemType)
            {
                case 事项类型.钉办督导:
                    var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == SuperviseID);
                    if (s != null && s.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
                case 事项类型.申请协调:
                    var r = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CooperativeID == CoordinateID);
                    if (r != null && r.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
                case 事项类型.精准督查:
                    var e = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == eSuperviseID);
                    if (e != null && e.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
                case 事项类型.专项督查:
                    var rs = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == sSuperviseID);
                    if (rs != null && rs.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
            }
            var lst = new List<Yqd_JobPlan>();
            foreach (var info in datas)
            {
                var status = (任务计划完成状态)int.Parse(info["Status"].ToString());
                var model = new Yqd_JobPlan
                {
                    TaskID = TaskID,
                    ItemID = ItemID,
                    ItemType = ItemType,
                    AssignID = AssignID,
                    Measure = info["Measure"].ToString(),
                    Leader = info["Leader"].ToString(),
                    Linkman = info["Linkman"]?.ToString() ?? "",
                    Status = status,
                    CheckStatus = 审核状态.待审,
                    IsComplete = false,
                    Instime = DateTime.Now,
                    EmployeeCode = WxUser.LogonInfo.EmployeeCode,
                    UnitCode = WxUser.LogonInfo.UnitCode
                };
                if (status == 任务计划完成状态.待办理)
                {
                    model.PlanCompleteTime = DateTime.Parse(info["PlanCompleteTime"].ToString());
                }
                lst.Add(model);
            }
            ctx.Yqd_JobPlan.AddRange(lst);
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                var (tblname, keyname, IsDeal) = SuperviseService.GetTableName(ItemType);
                ctx.Database.ExecuteSql("Update Yqd_" + tblname + " set Status={1}" + (IsDeal ? ",IsDeal=1" : "") + " Where " + keyname + "ID={0};", ItemID, (int)办理情况.已回复);
                var task = ctx.Yqd_Task.FirstOrDefault(it => it.TaskID == TaskID);
                if (task != null)
                {
                    var l = WxUser.LogonInfo;
                    var msg = ddService.Notification("", headtitle, task.TaskName, text + "\n" + l.UnitName + "已经办理反馈了，请您及时审核！", IsDd);
                    Logs.Warning<WxMySuperviseController>(l.Name + "发了工作通知，返回值：" + msg);
                }
                TaskLogService.AddLog(TaskID, "反馈了" + ItemType.ToString() + "任务清单");
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 办结反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [User]
        public IActionResult AddProgress(Yqd_PlanProgress model)
        {
            var SuperviseID = Form.GetInt("SuperviseID");
            var CoordinateID = Form.GetInt("CoordinateID");
            var eSuperviseID = Form.GetInt("eSuperviseID");
            var sSuperviseID = Form.GetInt("sSuperviseID");
            var ItemID = SuperviseID;
            var ItemType = 事项类型.钉办督导;
            string headtitle = "", text = "";
            var IsDd = false;
            var tblname = "";
            if (SuperviseID > 0)
            {
                var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                if (s != null)
                {
                    headtitle = "领导督导";
                    text = "督导意见  " + s.Content;
                }
            }
            if (CoordinateID > 0)
            {
                ItemID = CoordinateID;
                ItemType = 事项类型.申请协调;
                var c = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CoordinateID == ItemID);
                if (c != null)
                {
                    headtitle = "领导协调";
                    text = "协调内容  " + c.Item;
                }
            }
            if (eSuperviseID > 0)
            {
                ItemID = eSuperviseID;
                ItemType = 事项类型.精准督查;
                IsDd = true;
                tblname = "ExactSupervise";
                var s = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                if (s != null)
                {
                    headtitle = "精准督查";
                    text = "存在问题  " + s.Problem;
                }
            }
            if (sSuperviseID > 0)
            {
                ItemID = sSuperviseID;
                ItemType = 事项类型.专项督查;
                IsDd = true;
                tblname = "SpecialSupervise";
                var s = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                if (s != null)
                {
                    headtitle = "专项督查";
                    text = "存在问题  " + s.Problem;
                }
            }
            switch (ItemType)
            {
                case 事项类型.钉办督导:
                    var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == SuperviseID);
                    if (s != null && s.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
                case 事项类型.申请协调:
                    var r = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CooperativeID == CoordinateID);
                    if (r != null && r.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
                case 事项类型.精准督查:
                    var e = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == eSuperviseID);
                    if (e != null && e.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
                case 事项类型.专项督查:
                    var rs = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == sSuperviseID);
                    if (rs != null && rs.Status == 办理情况.已完结)
                    {
                        return this.ToJson(new
                        {
                            success = true,
                            ret = 0
                        });
                    }
                    break;
            }
            model.ItemID = ItemID;
            model.ItemType = ItemType;
            model.CheckStatus = 审核状态.待审;
            model.Instime = DateTime.Now;
            model.EmployeeCode = WxUser.LogonInfo.EmployeeCode;
            model.UnitCode = WxUser.LogonInfo.UnitCode;
            ctx.Yqd_PlanProgress.Add(model);
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                if (tblname != "")
                {
                    ctx.Database.ExecuteSql("Update Yqd_" + tblname + " set IsDeal=1 Where SuperviseID={0};", ItemID);
                }
                var task = ctx.Yqd_Task.FirstOrDefault(it => it.TaskID == model.TaskID);
                if (task != null)
                {
                    var plan = ctx.Yqd_JobPlan.FirstOrDefault(it => it.PlanID == model.PlanID);
                    if (plan != null)
                    {
                        text += "\n办理反馈  " + plan.Measure;
                    }
                    var l = WxUser.LogonInfo;
                    var msg = ddService.Notification("", headtitle, task.TaskName, text + "\n" + l.UnitName + "已经办结反馈了，请您及时审核！", IsDd);
                    Logs.Warning<WxMySuperviseController>(l.Name + "发了工作通知，返回值：" + msg);
                }
                TaskLogService.AddLog(model.TaskID, "反馈了任务办结情况");
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 审核办结反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [User]
        public IActionResult CheckProgress()
        {
            var PlanID = Form.GetInt("PlanID");
            var Status = (审核状态)Form.GetInt("Status");
            var progress = ctx.Yqd_PlanProgress.Where(it=>it.PlanID == PlanID && it.CheckStatus == Enm.审核状态.待审);
            long TaskID = 0;
            string unit = "", LeadUnit = "";
            var ItemID = 0;
            事项类型 ItemType = 0; 
            任务计划办理情况 PlanStatus = 0;
            foreach (var p in progress.OrderByDescending(it=>it.Instime))
            {
                if (TaskID == 0)
                {
                    TaskID = p.TaskID;
                    ItemID = p.ItemID;
                    ItemType = p.ItemType;
                    PlanStatus = p.PlanStatus;
                    LeadUnit = p.UnitCode;
                    unit = ctx.Yqd_Unit.FirstOrDefault(it => it.UnitCode == p.UnitCode)?.Name;
                }
                p.CheckStatus = Status;
                if (Status == 审核状态.退回)
                {
                    p.ReturnReason = Form.GetString("ReturnReason");
                }
                p.CheckTime = DateTime.Now;
            }
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                if(Status == 审核状态.同意 && PlanStatus == 任务计划办理情况.申请办结)
                {
                    ctx.Database.ExecuteSql("update Yqd_JobPlan set IsComplete=1 where PlanID={0}", PlanID);
                }
                string headtitle = "", text = "";
                var IsDd = false;
                switch (ItemType)
                {
                    case 事项类型.钉办督导:
                        {
                            var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                            if (s != null)
                            {
                                headtitle = "领导督导";
                                text = "督导意见  " + s.Content;
                            }
                        }
                        break;
                    case 事项类型.申请协调:
                        {
                            var c = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CoordinateID == ItemID);
                            if (c != null)
                            {
                                headtitle = "领导协调";
                                text = "协调内容  " + c.Item;
                            }
                        }
                        break;
                    case 事项类型.精准督查:
                        {
                            IsDd = true;
                            var s = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                            if (s != null)
                            {
                                headtitle = "精准督查";
                                text = "存在问题  " + s.Problem;
                            }
                        }
                        break;
                    case 事项类型.专项督查:
                        {
                            IsDd = true;
                            var s = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == ItemID);
                            if (s != null)
                            {
                                headtitle = "专项督查";
                                text = "存在问题  " + s.Problem;
                            }
                        }
                        break;
                }
                var plan = ctx.Yqd_JobPlan.FirstOrDefault(it => it.PlanID == PlanID);
                if (plan != null)
                {
                    text += "\n办理反馈  " + plan.Measure;
                }
                var task = ctx.Yqd_Task.FirstOrDefault(it => it.TaskID == TaskID);
                if (task != null)
                {
                    var l = WxUser.LogonInfo;
                    var msg = ddService.Notification(LeadUnit, headtitle, task.TaskName, text + "\n" + l.Name + "(" + l.UnitName + ")已经同意了您的办结反馈！", IsDd);
                    Logs.Warning<WxMySuperviseController>(l.Name + "发了工作通知，返回值：" + msg);
                }
                TaskLogService.AddLog(TaskID, "审核了" + unit + "任务办结情况");
                SuperviseService.UpdateComplete(ItemID, ItemType);
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 一键审核办结反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [User]
        public IActionResult CheckAllProgress()
        {
            var AssignID = Form.GetInt("AssignID");
            var progress = ctx.Yqd_PlanProgress.Where(it => it.AssignID == AssignID && it.CheckStatus == Enm.审核状态.待审);
            var pIDs = new List<int>();
            foreach (var p in progress.OrderByDescending(it => it.Instime))
            {
                p.CheckStatus = 审核状态.同意;
                p.CheckTime = DateTime.Now;
                if (p.PlanStatus == 任务计划办理情况.申请办结) pIDs.Add(p.PlanID);
            }
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                if (pIDs.Count>0)
                {
                    ctx.Database.ExecuteSql(string.Format("update Yqd_JobPlan set IsComplete=1 where PlanID in({0})", string.Join(",", pIDs)));
                }
                var assign = ctx.Yqd_Assign.FirstOrDefault(it => it.AssignID == AssignID);
                if (assign != null)
                {
                    string headtitle = "", text = "";
                    var IsDd = false;
                    var tblname = "";
                    var ItemID = 0;
                    switch (assign.ItemType)
                    {
                        case 事项类型.钉办督导:
                            {
                                var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == assign.ItemID);
                                if (s != null)
                                {
                                    headtitle = "领导督导";
                                    text = "督导意见  " + s.Content;
                                }
                            }
                            break;
                        case 事项类型.申请协调:
                            {
                                var c = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CoordinateID == assign.ItemID);
                                if (c != null)
                                {
                                    headtitle = "领导协调";
                                    text = "协调内容  " + c.Item;
                                }
                            }
                            break;
                        case 事项类型.精准督查:
                            {
                                IsDd = true;
                                tblname = "ExactSupervise";
                                ItemID = assign.ItemID;
                                var s = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == assign.ItemID);
                                if (s != null)
                                {
                                    headtitle = "精准督查";
                                    text = "存在问题  " + s.Problem;
                                }
                            }
                            break;
                        case 事项类型.专项督查:
                            {
                                IsDd = true;
                                tblname = "SpecialSupervise";
                                ItemID = assign.ItemID;
                                var s = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == assign.ItemID);
                                if (s != null)
                                {
                                    headtitle = "专项督查";
                                    text = "存在问题  " + s.Problem;
                                }
                            }
                            break;
                    }
                    if (tblname != "")
                    {
                        ctx.Database.ExecuteSql("Update Yqd_" + tblname + " set IsDeal=0 Where SuperviseID={0};", ItemID);
                    }
                    var task = ctx.Yqd_Task.FirstOrDefault(it => it.TaskID == assign.TaskID);
                    if (task != null)
                    {
                        var l = WxUser.LogonInfo;
                        var msg = ddService.Notification(assign.LeadUnit, headtitle, task.TaskName, text + "\n" + l.Name + "(" + l.UnitName + ")已经全部同意了您的办结反馈！", IsDd);
                        Logs.Warning<WxMySuperviseController>(l.Name + "发了工作通知，返回值：" + msg);
                    }
                    TaskLogService.AddLog(assign.TaskID, "审核了" + ctx.Yqd_Unit.FirstOrDefault(it => it.UnitCode == assign.UnitCode)?.Name + "任务办结情况");
                    SuperviseService.UpdateComplete(assign.ItemID, assign.ItemType);
                }
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
        
        /// <summary>
        /// 审核办理计划
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [User]
        public IActionResult CheckPlan()
        {
            var TaskID = Form.GetLong("TaskID");
            var AssignID = Form.GetInt("AssignID");
            var Status = Form.GetInt("Status");
            var Reason = Form.GetString("ReturnReason");
            var sql = "update Yqd_JobPlan set CheckStatus={1},CheckTime=getdate()";
            var bl = (审核状态)Status == 审核状态.退回;
            if (bl)
            {
                sql += ",ReturnReason={2}";
            }
            else
            {
                sql += ",IsComplete=(case Status when 20 then 1 else 0 end)";
            }
            sql += " where AssignID={0} and CheckStatus={3};";
            var ret = ctx.Database.ExecuteSql(sql, AssignID, Status, Reason,(int)审核状态.待审);
            if (ret > 0)
            {
                var unit = "";
                var assign = ctx.Yqd_Assign.FirstOrDefault(it => it.AssignID == AssignID);
                if (assign != null)
                {
                    var (tblname, keyname, IsDeal) = SuperviseService.GetTableName(assign.ItemType);
                    unit = ctx.Yqd_Unit.FirstOrDefault(it => it.UnitCode == assign.LeadUnit)?.Name;
                    if (!bl) {
                        var nt = DateTime.Now.Date; 
                        var fl = ctx.Yqd_JobPlan.Any(it => it.AssignID == assign.AssignID && it.CheckStatus == 审核状态.同意 && (it.PlanCompleteTime != null && it.PlanCompleteTime < nt));
                        ctx.Database.ExecuteSql("Update Yqd_" + tblname + " set Status={1}" + (IsDeal ? ", IsDeal = 0" : "") + " Where " + keyname + "ID={0}", assign.ItemID, fl?(int)办理情况.已滞后:(int)办理情况.已回复);
                        SuperviseService.UpdateComplete(assign.ItemID, assign.ItemType);
                    }
                    else
                    {
                        ctx.Database.ExecuteSql("Update Yqd_" + tblname + " set Status={1}"+(IsDeal ? ", IsDeal = 0" : "")+" Where " + keyname + "ID={0}", assign.ItemID, (int)办理情况.未回复);
                    }
                    string headtitle = "", text = "";
                    var IsDd = false;
                    switch(assign.ItemType)
                    {
                        case 事项类型.钉办督导:
                            {
                                var s = ctx.Yqd_Supervise.FirstOrDefault(it => it.SuperviseID == assign.ItemID);
                                if (s != null)
                                {
                                    headtitle = "领导督导";
                                    text = "督导意见  " + s.Content;
                                }
                            }
                            break;
                        case 事项类型.申请协调:
                            {
                                var c = ctx.Yqd_Coordinate.FirstOrDefault(it => it.CoordinateID == assign.ItemID);
                                if (c != null)
                                {
                                    headtitle = "领导协调";
                                    text = "协调内容  " + c.Item;
                                }
                            }
                            break;
                        case 事项类型.精准督查:
                            {
                                IsDd = true;
                                var s = ctx.Yqd_ExactSupervise.FirstOrDefault(it => it.SuperviseID == assign.ItemID);
                                if (s != null)
                                {
                                    headtitle = "精准督查";
                                    text = "存在问题  " + s.Problem;
                                }
                            }
                            break;
                        case 事项类型.专项督查:
                            {
                                IsDd = true;
                                var s = ctx.Yqd_SpecialSupervise.FirstOrDefault(it => it.SuperviseID == assign.ItemID);
                                if (s != null)
                                {
                                    headtitle = "专项督查";
                                    text = "存在问题  " + s.Problem;
                                }
                            }
                            break;
                    }
                    var task = ctx.Yqd_Task.FirstOrDefault(it => it.TaskID == TaskID);
                    if (task != null)
                    {
                        var l = WxUser.LogonInfo;
                        var msg = ddService.Notification(assign.LeadUnit, headtitle, task.TaskName, text + "\n" + l.Name + "(" + l.UnitName + ")已经" + (bl ? "退回" : "同意") + "了您反馈的任务清单！", IsDd);
                        Logs.Warning<WxMySuperviseController>(l.Name + "发了工作通知，返回值：" + msg);
                    }
                }
                
                TaskLogService.AddLog(TaskID, "审核了" + unit + "任务清单");
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult GetPlanHistory()
        {
            var AssignID = Form.GetInt("AssignID");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var r = ctx.Yqd_JobPlan.Where(it => it.AssignID == AssignID && it.CheckStatus == Enm.审核状态.退回);
            r = r.OrderByDescending(it => it.Instime);
            var totalcount = r.Count();
            if (pagesize > 0)
            {
                r = r.Skip((page - 1) * pagesize).Take(pagesize);
            }
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = r.ToList().Select(it => new
                {
                    it.PlanID,
                    it.Measure,
                    it.Leader,
                    it.Linkman,
                    Status = Enum.GetName(typeof(Enm.任务计划完成状态), it.Status),
                    PlanCompleteTime = it.PlanCompleteTime != null ? it.PlanCompleteTime.ToString("yyyy.MM.dd") : "",
                    it.ReturnReason
                })
            });
        }

        [User]
        public ActionResult RepealPlan()
        {
            var PlanID = Form.GetInt("PlanID");
            var ret = ctx.Database.ExecuteSql("update Yqd_JobPlan set CheckStatus={1},ReturnReason=null,CheckTime=null where PlanID={0}", PlanID, (int)审核状态.待审);
            if (ret > 0)
            {
                var plan = ctx.Yqd_JobPlan.FirstOrDefault(it => it.PlanID == PlanID);
                if (plan != null)
                {
                    var (tblname, keyname, IsDeal) = SuperviseService.GetTableName(plan.ItemType);
                    ctx.Database.ExecuteSql("Update Yqd_" + tblname + " set Status={1}" + (IsDeal ? ", IsDeal = 1" : "") + " Where " + keyname + "ID={0}", plan.ItemID, (int)办理情况.已回复);
                }
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult GetProgressHistory()
        {
            var PlanID = Form.GetInt("PlanID");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var r = ctx.Yqd_PlanProgress.Where(it => it.PlanID == PlanID && it.CheckStatus == Enm.审核状态.退回);
            r = r.OrderByDescending(it => it.Instime);
            var totalcount = r.Count();
            if (pagesize > 0)
            {
                r = r.Skip((page - 1) * pagesize).Take(pagesize);
            }
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = r.ToList().Select(it => new
                {
                    it.ProgressID,
                    PlanStatus = it.PlanStatus.ToString(),
                    PlanResult = it.PlanResult ?? "",
                    DelayReason = it.DelayReason ?? "",
                    DelayTime = it.DelayTime != null ? it.DelayTime.ToString("yyyy.MM.dd") : "",
                    it.ReturnReason
                })
            });
        }

        [User]
        public ActionResult RepealProgress()
        {
            var ProgressID = Form.GetInt("ProgressID");
            var ret = ctx.Database.ExecuteSql("update Yqd_PlanProgress set CheckStatus={1},ReturnReason=null,CheckTime=null where ProgressID={0}", ProgressID, (int)审核状态.待审);
            if (ret > 0)
            {
                var p = ctx.Yqd_PlanProgress.FirstOrDefault(it => it.ProgressID == ProgressID);
                if (p != null && (p.ItemType == 事项类型.精准督查 || p.ItemType == 事项类型.专项督查))
                {
                    ctx.Database.ExecuteSql("Update Yqd_" + (p.ItemType == 事项类型.精准督查 ? "Exact" : "Special") + "Supervise set IsDeal=1 Where SuperviseID={0};", p.ItemID);
                }
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
